import { Component } from '@angular/core';
import { IonicPage, NavParams, NavController, Events } from 'ionic-angular';

import { GlobalData } from "../../providers/GlobalData";

/**
 * Generated class for the ProductOptionListPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-product-option-list',
  templateUrl: 'product-option-list.html',
})
export class ProductOptionListPage {

  productOptions: Array<{
    id: number,
    dictionary: number,
    name: string,
    price: string,
    sign: string,
    qty: number,
    type: string,
    selected: boolean
  }>;

  constructor(public navCtrl: NavController,
    public navParams: NavParams,
    public events: Events,
    public global: GlobalData) {
    console.log(this.navParams);
    this.productOptions = [];

    for (let option of this.navParams['data']['options']) {
      this.productOptions.push({
        id: option['id'],
        dictionary: this.navParams['data']['dictionary'],
        name: option['name'],
        price: option['price'],
        sign: option['sign'],
        qty: option['qty'],
        type: option['type'],
        selected: option['selected']
      });
    }

  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ProductOptionListPage');
  }

  selectOption(option) {
    if (option['type'] == 'spec') {
      for (let spe of this.productOptions) {
        if (spe['type'] == 'spec') {
          spe['selected'] = false;
        }
      }
    }
    option['qty'] = 1;
    option.selected = !option.selected;
    this.events.publish('editcart:selectoption', option, Date.now());
  }

  resetOption(option) {
    if (option['type'] == 'addons') {
      option['qty'] = 0;
      option.selected = !option.selected;
      this.events.publish('editcart:selectoption', option, Date.now());
      return;
    }
    option['qty'] = 0;
    option.selected = !option.selected;
    this.productOptions[0]['selected'] = true;
    this.events.publish('editcart:selectoption', this.productOptions[0], Date.now());
  }
}
